package com.dzt.androidframework.JsDemo;

import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.Toast;

import com.dzt.androidframework.R;
import com.dzt.androidframework.activity.ActivityTitle;

import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

public class JsActivity extends ActivityTitle {

	@ViewInject(R.id.webview)
	private WebView contentWebView;

	@Override
	protected int getLayoutId() {
		return R.layout.activity_js;
	}

	@Override
	protected void initParams() {
		setTitleView(R.string.menu_9);
		// 启用javascript
		contentWebView.getSettings().setJavaScriptEnabled(true);
		// 从assets目录下面的加载html
		contentWebView.loadUrl("file:///android_asset/web.html");
		contentWebView.addJavascriptInterface(JsActivity.this, "android");
	}

	@Override
	protected void initData(Bundle savedInstanceState) {

	}

	@Event(value = {R.id.btn_call_js, R.id.btn_call_param_js})
	private void onViewClick(View v) {
		switch (v.getId()) {
			case R.id.btn_call_js:
				// 无参数调用
				contentWebView.loadUrl("javascript:javacalljs()");
				break;
			case R.id.btn_call_param_js:
				// 传递参数调用
				contentWebView.loadUrl("javascript:javacalljswith(" + "'http://blog.csdn.net/Leejizhou'" + ")");
				break;
			default:
				break;
		}
	}

	@Override
	protected String[] initPermissions() {
		return new String[0];
	}

	//由于安全原因 需要加 @JavascriptInterface
	@JavascriptInterface
	public void startFunction() {

		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				Toast.makeText(JsActivity.this, "show", Toast.LENGTH_LONG).show();

			}
		});
	}

	@JavascriptInterface
	public void startFunction(final String text) {
		runOnUiThread(new Runnable() {

			@Override
			public void run() {
				new AlertDialog.Builder(JsActivity.this).setMessage(text).show();
			}
		});
	}
}
